package com.alliky.core.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.alliky.core.api.BaseApi;
import com.alliky.core.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.File;

public class GlideUtils {
    /**
     * DiskCacheStrategy.NONE 什么都不缓存
     * DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像
     * DiskCacheStrategy.RESULT 仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
     * DiskCacheStrategy.ALL 缓存所有版本的图像（默认行为）
     *
     * @param context
     * @param url
     * @param imageView
     * @param placeholderRes
     * @param errorRes
     */
    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        Glide.with(context)
                .load(getImageUrl(url))
                .placeholder(placeholderRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImage(Context context, File file, ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        Glide.with(context)
                .load(file)
                .placeholder(placeholderRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        Glide.with(context)
                .load(bitmap)
                .placeholder(placeholderRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImage(Context context, Drawable drawable, ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        Glide.with(context)
                .load(drawable)
                .placeholder(placeholderRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImage(Context context, Uri uri, ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        Glide.with(context)
                .load(uri)
                .placeholder(placeholderRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int placeholderRes) {
        loadImage(context, url, imageView, placeholderRes, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, File file, ImageView imageView, @DrawableRes int placeholderRes) {
        loadImage(context, file, imageView, placeholderRes, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView, @DrawableRes int placeholderRes) {
        loadImage(context, bitmap, imageView, placeholderRes, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, Drawable drawable, ImageView imageView, @DrawableRes int placeholderRes) {
        loadImage(context, drawable, imageView, placeholderRes, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, Uri uri, ImageView imageView, @DrawableRes int placeholderRes) {
        loadImage(context, uri, imageView, placeholderRes, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context, url, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }

    public static void loadImage(Context context, File file, ImageView imageView) {
        loadImage(context, file, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }


    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView) {
        loadImage(context, bitmap, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }


    public static void loadImage(Context context, Drawable drawable, ImageView imageView) {
        loadImage(context, drawable, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }


    public static void loadImage(Context context, Uri uri, ImageView imageView) {
        loadImage(context, uri, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }

    private static String getImageUrl(String url) {
        if (!TextUtils.isEmpty(url) && url.startsWith("http")) {
            return url;
        } else {
            return BaseApi.BASE_IMAGE + url;
        }
    }
}
